package pkg_member

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/pkg_member"
	"gorm.io/gorm"
)

// DeleteJoinactivateByActivityAndUser 删除特定活动和用户的参加活动记录
// Author [piexlmax](https://github.com/piexlmax)
func (joinactivateService *JoinactivateService) DeleteJoinactivateByActivityAndUser(activateID int, userID int) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		// 执行删除操作
		if err := tx.Delete(&pkg_member.Joinactivate{}, "activateid = ? AND userid = ?", activateID, userID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

func (joinactivateService *JoinactivateService) SearchCurrentIsJoinActivate(activateID int, userID int) (bool, error) {
	var joinactivate pkg_member.Joinactivate
	err := global.GVA_DB.First(&joinactivate, "activateid = ? AND userid = ?", activateID, userID).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			// 没有找到匹配的记录，返回false
			return false, nil
		}
		// 数据库查询出错，返回错误
		return false, err
	}
	// 找到了匹配的记录，返回true
	return true, nil
}
